package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.ReceiveAddress;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.ReceiveAddressMapper;
import org.boot.ssm.wxshop.service.ReceiveAddressService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: wxshop
 * @description: 收货地址业务实现
 * @author: 刘涛
 * @create: 2018-03-21 14:38
 **/
@Service
public class ReceiveAddressServiceImpl extends BaseServiceImpl<ReceiveAddress> implements ReceiveAddressService{

    @Autowired
    private ReceiveAddressMapper receiveAddressMapper;
    /**
     * 添加或修改收货地址
     * @param receiveAddress
     * @param user
     * @return
     */
    @Override
    public JsonResult addOrUpdateReceiveAddress(ReceiveAddress receiveAddress, User user) {
            Integer isUpdate;
            if(receiveAddress.getIsDefault()==1){
                receiveAddressMapper.updateIsDefault(user.getId());
            }
            if(receiveAddress.getId()==null){
                receiveAddress.setUserId(user.getId());
                isUpdate = this.saveNoNull(receiveAddress);
                if(isUpdate==1){
                    return new JsonResult(200,"添加成功");
                }else {
                    throw new BusinessException("添加失败");
                }
            }else {
                receiveAddress.setUserId(user.getId());
                isUpdate =this.updateNoNull(receiveAddress);
                if(isUpdate==1){
                    return new JsonResult(200,"修改成功");
                }else {
                    throw new BusinessException("修改失败");
                }

            }

    }

    /**
     * 逻辑删除用户收货地址
     * @param receiveAddress
     * @return
     */
    @Override
    public JsonResult deleteAddress(ReceiveAddress receiveAddress) {
        Integer isDelete;
        if(receiveAddress.getIsDefault()==1){
            throw new BusinessException("无法删除默认地址");
        }else {
            receiveAddress.setIsDelete(0);
            isDelete = this.updateNoNull(receiveAddress);
            if(isDelete==1){
                return new JsonResult(200,"删除成功");
            }else {
                throw new BusinessException("删除失败");
            }

        }
    }
}
